import axios, { AxiosError, AxiosRequestConfig, AxiosResponse, AxiosInstance } from 'axios'
import store from '@/store'
import {ElLoading, ElMessage} from 'element-plus'
const baseURL = import.meta.env.VITE_BASE_URL
import { useRouter } from 'vue-router'

const service = axios.create({
  baseURL: baseURL,
  timeout: 15000
})

// 请求前的统一处理
service.interceptors.request.use(
  (config) => {
    // JWT鉴权处理
    const token = localStorage.getItem('token')
    if (store.getters['user/token']) {
      config.headers['token'] = token
    }

    config.headers['Content-Type'] = config.url=='/api/oss/batchUpload'?'multipart/form-data':'application/json'
    // console.log(config,999)
    return config
  },
  (error) => {
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

service.interceptors.response.use(
  (response) => {
    const res = response.data
    if (res.code === 0) {
      return res
     }else if(res.code == 2001){

      showError(res)
     }
    else {
        const loadingInstance = ElLoading.service({fullscreen: true})
        loadingInstance.close()
      showError(res)
      return Promise.reject(res)
    }
  },
  (error) => {
    console.log(error) // for debug
    const badMessage = error.message || error
    const code = parseInt(badMessage.toString().replace('Error: Request failed with status code ', ''))
    showError({ code, message: badMessage })
    return Promise.reject(error)
  }
)

// 错误处理
function showError(error) {
  // token过期，清除本地数据，并跳转至登录页面
  if (error.code ===2001) {
    // to re-login
    store.dispatch('user/loginOut')
  } else {
    ElMessage({
      message: error.msg || error.message || '服务异常',
      type: 'error',
      duration: 3 * 1000
    })
  }

}

export default service